#include "colorpad.h"


colorpad::colorpad(void)
{
		colors[0]=vec3(.9,.9,.9);
		colors[1]=vec3(0.4,0.4,0);
		colors[2]=vec3(0,0,0.4);
		colors[3]=vec3(0,0.4,0);
		colors[4]=vec3(.8,.35,0);
		colors[5]=vec3(.4,0,0);

}


//x y screen coordinates: (0,0) on topleft
//topleft world coordinates: (0,0) in the middle
bool intersect(vec2 topleft, float w, float h, float x, float y){
	float ww = glutGet(GLUT_WINDOW_WIDTH);
	float wh = glutGet(GLUT_WINDOW_HEIGHT);

	x = x - ww/2;
	y = -y + wh/2;
	//cout << "x: " << x << " y: " << y << endl;

	float topx = topleft[0];
	float topy = topleft[1];
	//cout << "topx: " << topx << " topy: " << topy << endl;
	return ( x >= topx && x <= topx+w) && ( y >= topy-h && y <= topy);
}

vec3* colorpad::setColor(int x, int y){
	for (int i=0;i<6;i++){	
		if (intersect(positions[i],facetw,faceth,x,y)){
			glColor3f(colors[i][0],colors[i][1],colors[i][2]);
			cout << "Color set to " << colors[i] << endl;
			return &colors[i];
		}
	}
	return NULL;
}

/*
void
print_bitmap_string(void* font, char* s)
{
   if (s && strlen(s)) {
      while (*s) {
         glutBitmapCharacter(font, *s);
         s++;
      }
   }
}

void
print_stroke_string(void* font, char* s)
{
   if (s && strlen(s)) {
      while (*s) {
         glutStrokeCharacter(font, *s);
         s++;
      }
   }
}
*/
void colorpad::render(){
	float ww = glutGet(GLUT_WINDOW_WIDTH);
	float wh = glutGet(GLUT_WINDOW_HEIGHT);
	
	ww = min(ww,wh);
	wh = ww;

	float ws = 2*1.25;
	float w = ww/4/3;
	float h = wh/4/3;
	this->facetw = w;
	this->faceth = h;

	float y = -wh/2 + h;
	float x = ww/2 - 6*w;
	//glColor4f(0,1,0,0);
	//print_stroke_string(GLUT_STROKE_ROMAN, "HELLO");

	//glRasterPos2f(10,10);
	//print_bitmap_string(GLUT_BITMAP_TIMES_ROMAN_10, "HI");
	//glColor4f(0,0,0,0);
	glEnable (GL_LINE_STIPPLE);
	glLineWidth(3.90);
	
	for (int i=0;i<6;i++){
		glColor3f(colors[i][0],colors[i][1],colors[i][2]);
		glBegin(GL_QUADS);
		 glVertex2f(ws*x/ww,ws*y/wh);
		 glVertex2f(ws*(x+w)/ww,ws*y/wh);
		 glVertex2f(ws*(x+w)/ww,ws*(y-h)/wh);
		 glVertex2f(ws*x/ww,ws*(y-h)/wh);
		glEnd();
		positions[i]=vec2(x,y);
		x += w;
	}
	
}
colorpad::~colorpad(void)
{
}
